package com.example.eos.controller;

import com.example.eos.bean.ReturnInfo;
import com.example.eos.bean.User;
import com.example.eos.service.FilesService;
import com.example.eos.tool.GlobalParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class FilesController {

    private FilesService filesService;

    @Autowired
    public FilesController(FilesService filesService){
        this.filesService = filesService;
    }

    //作品图片上传
    @RequestMapping(value = "/public/pictures", method = RequestMethod.POST)
    public ReturnInfo uploadPictures(@RequestParam("file") MultipartFile[] files){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String filepath = user.getUserid()+"/"+GlobalParam.getSTime();
        List<String> stringList = new ArrayList<>();
        if(files.length>0){
            for(MultipartFile file : files){
                stringList.add(filesService.uploadFile(file,filepath+"/bp-"+GlobalParam.getSsTime()));
            }
        }
        return ReturnInfo.info3(stringList);
    }

    //用户头像上传
    @RequestMapping(value = "/user/headpicture", method = RequestMethod.POST)
    public ReturnInfo uploadHeadPicture(@RequestParam("file") MultipartFile file){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String filepath = user.getUserid()+"/"+"user/headpicture";
        return ReturnInfo.info1(filesService.uploadFile(file, filepath));
    }

//    //文件下载
//    @RequestMapping(value = "/files", method = RequestMethod.GET)
//    public ReturnInfo downloadFile(){
//
//    }

}
